import request from '@/utils/request'
import { useuserstore } from '@/stores/modules/user'
import { ElMessage } from 'element-plus'
let userstore = useuserstore()
// 清空userinfo
// userstore.clearuserinfo()
// 注册请求,第二个对象将作为body传递
export const userregisterservice = ({ username, password }) => {
  console.log(userstore.userinfo)
  userstore.userinfo.code = -1
  let flag = true
  userstore.userinfo.username.forEach((ele) => {
    if (ele == username) {
      flag = false
      ElMessage.warning('用户名重复')
      return
    }
  })
  console.log(flag)
  if (flag) {
    userstore.userinfo.code = 0
    userstore.userinfo.username.push(username)
    userstore.userinfo.password.push(password)
    console.log('ok')
  }
  return request.post('/api/register', userstore.userinfo)
}
// 登录请求
export const userloginservice = ({ username, password }) => {
  userstore.userinfo.code = -1
  // 拿到请求体中的用户名和密码到userinfo查找
  userstore.userinfo.username.forEach((ele) => {
    if (ele == username) {
      userstore.userinfo.password.forEach((ele) => {
        if (ele == password) {
          userstore.userinfo.code = 0
          return
        }
      })
    }
  })
  return request.post('/api/login', userstore.userinfo)
}
// 获取用户信息
export const useuserinfoservice = () => {
  return userstore.userinfo
}
